package com.badibadi.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.badibadi.activity.CreationNewClubActivity;
import com.badibadi.activity.LogReadingActivity;
import com.badibadi.activity.LogReadingDanjiActivity;
import com.badibadi.activity.LookAtTheDetailedActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.PicturePreviewActivity;
import com.badibadi.activity.PinyinSeeOtherPeopleFriendActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.infos.AtModel;
import com.badibadi.infos.BrowseAllPhoto_Model;
import com.badibadi.infos.ClubApplyModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;
//about_me_invite_type_name about_me_invite_type_name about_me_invite_common about_me_invite_type_name
//@好友 about_me_invite_time

public class AfterLandingAddFriendsMsgItemFragment2 extends BaseAppFragment
		implements IXListViewListener {
	private XListView2Adapter___1 x2Adapter;
	private XListView xListView2;
	private int Page;
	private Results results;
	private List<AtModel> friend_Models, list;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:

				try {
					Utils.ExitPrgress(getActivity());
					onLoad();
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				Utils.ExitPrgress(getActivity());
				try {
					friend_Models.addAll(list);
					x2Adapter.notifyDataSetChanged();
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(getActivity());

				try {
					onLoad();
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		friend_Models = new ArrayList<AtModel>();
		x2Adapter = new XListView2Adapter___1();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.after_landing_freinds00_1, null);
		InitMyXListView2(view);
		return view;
	}

	public void InitMyXListView2(View view) {
		Page = 0;
		Constants.Invite_handler = mHandler;
		xListView2 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView2.setXListViewListener(this);
		xListView2.setPullLoadEnable(true);
		xListView2.setPullRefreshEnable(true);
		xListView2.setAdapter(x2Adapter);
		xListView2.startLoadMore();
	}

	private void Invite_announce(final int page) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<AtModel>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(getActivity()));
				map.put("type", 3);
				map.put("page", page);
				map.put("pageNum", 10);
				// map.put("languageType",
				// Dialog.getSystemLanguageType(getActivity()));
//				   
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "invite_announce");
				//    
				if (invite_content != null) {
					results = Utils.checkResult_NNN(getActivity(),
							invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						//    ("解析出来的数据:" + results.getRetmsg());
//						   
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(), AtModel.class);

							mHandler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}

			}
		});
	}

	protected void onLoad() {
		xListView2.stopRefresh();
		xListView2.stopLoadMore();
		xListView2.setRefreshTime(""+getResources().getString(R.string.ganggang));
	}

	class XListView2Adapter___1 extends BaseAdapter {
		private DisplayImageOptions options;

		public XListView2Adapter___1() {
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
		}

		@Override
		public int getCount() {
			return friend_Models.size();
		}

		@Override
		public Object getItem(int position) {
			return friend_Models.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		// about_me_invite_back_img about_me_invite_name
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			convertView = inflater.inflate(
					R.layout.fragment_afterlanding_tidaome_import_layout, null);
			ImageView about_me_invite_back_img = (ImageView) convertView
					.findViewById(R.id.about_me_invite_back_img);
			TextView about_me_invite_name = (TextView) convertView
					.findViewById(R.id.about_me_invite_name);
			TextView about_me_invite_type = (TextView) convertView
					.findViewById(R.id.about_me_invite_type);
			TextView about_me_invite_type_name = (TextView) convertView
					.findViewById(R.id.about_me_invite_type_name);
			TextView about_me_invite_common = (TextView) convertView
					.findViewById(R.id.about_me_invite_common);
			TextView about_me_invite_time = (TextView) convertView
					.findViewById(R.id.about_me_invite_time);
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl
							+ friend_Models.get(position).getHead()
							+ Constants.appPhoto4img, about_me_invite_back_img,
					options);
			about_me_invite_back_img
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (friend_Models.get(position).getId() != null) {
								if (!friend_Models
										.get(position)
										.getId()
										.equals(""
												+ Utils.getUid(getActivity()))) {
									Intent intent = new Intent(getActivity(),
											SeeOtherPeopleSpaceActivity.class);
									intent.putExtra("friend_id", friend_Models
											.get(position).getId());
									startActivity(intent);
								} else {
									Intent intent = new Intent(getActivity(),
											MyDataActivity.class);
									// intent.putExtra("friend_id",
									// friend_Models.get(position).getId());
									startActivity(intent);
								}
							}

						}
					});
			about_me_invite_name.setText(friend_Models.get(position)
					.getNickName());
			about_me_invite_name.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (friend_Models.get(position).getId() != null) {
						if (!friend_Models.get(position).getId()
								.equals("" + Utils.getUid(getActivity()))) {
							Intent intent = new Intent(getActivity(),
									SeeOtherPeopleSpaceActivity.class);
							intent.putExtra("friend_id",
									friend_Models.get(position).getId());
							startActivity(intent);
						} else {
							Intent intent = new Intent(getActivity(),
									MyDataActivity.class);
							// intent.putExtra("friend_id",
							// friend_Models.get(position).getId());
							startActivity(intent);
						}
					}
				}
			});
			if (friend_Models.get(position).getType_name() != null) {
				if (friend_Models.get(position).getType_name().equals("记录")) {
					about_me_invite_type.setText(getResources().getString(
							R.string.record));
				} else if (friend_Models.get(position).getType_name()
						.equals("图片")) {
					about_me_invite_type.setText(getResources().getString(
							R.string.picture));
				} else if (friend_Models.get(position).getType_name()
						.equals("动态")) {
					about_me_invite_type.setText(getResources().getString(
							R.string.trends));
				}
			}else{
				about_me_invite_type.setText("");
			}
			about_me_invite_common.setText(friend_Models.get(position)
					.getEqual());
			about_me_invite_common
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),
									PinyinSeeOtherPeopleFriendActivity.class);
							intent.putExtra("uid", friend_Models.get(position)
									.getId());
							intent.putExtra("fid", Utils.getUid(getActivity()));
							startActivity(intent);

						}
					});
			try {
				about_me_invite_time.setText(CalendarTools
						.getTimeChangeToString(getActivity(), friend_Models
								.get(position).getTime()));
			} catch (Exception e) {
				// TODO: handle exception
			}

			about_me_invite_type_name.setText(friend_Models.get(position)
					.getName());
			about_me_invite_type_name
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							String type_name = friend_Models.get(position)
									.getType_name();
							if (type_name.equals("记录")) {
								Intent intent = new Intent(getActivity(),
										LogReadingDanjiActivity.class);
								intent.putExtra("aid",
										friend_Models.get(position).getDid());
								startActivity(intent);
							} else if (type_name.equals("图片")) {
								Intent intent = new Intent(getActivity(),
										PicturePreviewActivity.class);
								// intent.putExtra("rid",
								// friend_Models.get(position).getDid());
								List<BrowseAllPhoto_Model> AllPhoto_Models = new ArrayList<BrowseAllPhoto_Model>();
								BrowseAllPhoto_Model br = new BrowseAllPhoto_Model();
								br.setId(friend_Models.get(position).getDid());
								br.setPath(friend_Models.get(position)
										.getPath());
								AllPhoto_Models.add(br);
								intent.putExtra("picture_list",
										(Serializable) AllPhoto_Models);
								startActivity(intent);
							} else if (type_name.equals("动态")) {
								Intent intent = new Intent(getActivity(),
										LookAtTheDetailedActivity.class);
								intent.putExtra("trendsId",
										friend_Models.get(position).getDid());
								startActivity(intent);
							}
						}
					});
			return convertView;
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Page = 1;
		friend_Models = new ArrayList<AtModel>();
		Invite_announce(Page);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Invite_announce(++Page);
	}
}